﻿/*
* Copyright 2010 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Criterion;
using Ngilead.Test.Models;
using Ngilead.Test.Models.Stateless;

namespace Ngilead.Test.DAO
{
    /// <summary>
    /// User data access class
    /// </summary>
    public class UserDAO : GenericDAO<User, Int64>, IUserDAO 
    {
        /// <summary>
        /// Load user by login
        /// </summary>
        /// <param name="login">Login</param>
        /// <returns>User</returns>
        public User LoadUserByLogin(String login)
        {
            User result = null;
            try
            {
                ICriterion criterion = Expression.Eq("Login", login);
                result = LoadByCriteria(criterion).FirstOrDefault();
            }
            catch (Exception ex)
            {
                throw new Exception(String.Format("Could not load user for login {0}", login), ex);
            }

            return result;
        }

        /// <summary>
        /// Load user and groups by login
        /// </summary>
        /// <param name="login">Login</param>
        /// <returns>User</returns>
        public User SearchUserAndGroupsByLogin(String login)
        {
            User result = null;
            ISession session = null;
            ITransaction transaction = null;
            
            try
            {
                session = GetSession();
                transaction = session.BeginTransaction();

                StringBuilder hqlQuery = new StringBuilder("FROM User user");
                hqlQuery.Append(" LEFT JOIN FETCH user.GroupList");
                hqlQuery.Append(" WHERE user.Login = :login");

                IQuery query = session.CreateQuery(hqlQuery.ToString());
                query.SetString("login", login);

                result = query.UniqueResult<User>();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                throw new Exception(String.Format("Could not load user and groups for login {0}", login), ex);
            }

            return result;
        }

        /// <summary>
        /// Load user and message by login
        /// </summary>
        /// <param name="login">Login</param>
        /// <returns>User</returns>
        public User SearchUserAndMessagesByLogin(String login)
        {
            User result = null;
            ISession session = null;
            ITransaction transaction = null;

            try
            {
                session = GetSession();
                transaction = session.BeginTransaction();

                StringBuilder hqlQuery = new StringBuilder("FROM User user");
                hqlQuery.Append(" LEFT JOIN FETCH user.MessageList");
                hqlQuery.Append(" WHERE user.Login = :login");

                IQuery query = session.CreateQuery(hqlQuery.ToString());
                query.SetString("login", login);

                result = query.UniqueResult<User>();
                transaction.Commit();

            }
            catch (Exception ex)
            {
                transaction.Rollback();
                throw new Exception(String.Format("Could not load user and messages for login {0}", login), ex);
            }

            return result;
        }

        /// <summary>
        /// Load user and group and message by login
        /// </summary>
        /// <param name="login">Login</param>
        /// <returns>User</returns>
        public User SearchUserAndGroupAndMessagesByLogin(String login)
        {
            User result = null;
            ISession session = null;
            ITransaction transaction = null;

            try
            {
                session = GetSession();
                transaction = session.BeginTransaction();

                StringBuilder hqlQuery = new StringBuilder("FROM User user");
                hqlQuery.Append(" LEFT JOIN FETCH user.MessageList");
                hqlQuery.Append(" LEFT JOIN FETCH user.GroupList");
                hqlQuery.Append(" LEFT JOIN FETCH user.Gender");
                hqlQuery.Append(" WHERE user.Login = :login");

                IQuery query = session.CreateQuery(hqlQuery.ToString());
                query.SetString("login", login);

                result = query.UniqueResult<User>();
                transaction.Commit();

            }
            catch (Exception ex)
            {
                transaction.Rollback();
                throw new Exception(String.Format("Could not load user and group and messages for login {0}", login), ex);
            }

            return result;
        }

        /// <summary>
        /// Load all users and messages
        /// </summary>
        /// <returns>Users</returns>
        public IList<User> LoadAllUserAndMessages()
        {
            IList<User> result = null;
            ISession session = null;
            ITransaction transaction = null;
            
            try
            {
                session = GetSession();
                transaction = session.BeginTransaction();

                IQuery query = session.CreateQuery("FROM User user LEFT JOIN FETCH user.MessageList");

                result = query.List<User>();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                throw new Exception("Could not load all users and messages", ex);
            }

            return result;
        }               
    }
}
